<template>
  <div class="containers">
    <el-card>
    <div class="">
      <div class="handle-box">
        <el-select v-model="state" @change="getDataPage" placeholder="请选择类型" size="mini" class="mr10">
          <el-option :key="-1" :label="'全部'" :value="-1"></el-option>
          <el-option :key="0" :label="'下架'" :value="0"></el-option>
          <el-option :key="1" :label="'上架'" :value="1"></el-option>
          <el-option :key="2" :label="'待审核'" :value="2"></el-option>
          <el-option :key="3" :label="'审核失败'" :value="3"></el-option>
        </el-select>
        <el-select v-model="code" @change="getDataPage" placeholder="请选择类型" size="mini" class="mr10">
          <el-option v-for="(item, index) in navlist" :key="item.ID" :label="item.name" :value="item.ID">
          </el-option>
        </el-select>
        <el-select v-model="class_first_id" @change="setfirst" placeholder="请选择一级分类" size="mini" class="mr10">
          <el-option v-for="(item, index) in firstlist" :key="item.ID" :label="item.name" :value="item.ID"></el-option>
        </el-select>
        <el-select v-model="class_second_id" placeholder="请选择二级分类" size="mini" class="mr10" @change="setsecond">
          <el-option v-for="(item, index) in secondlist" :key="item.ID" :label="item.name" :value="item.ID"></el-option>
        </el-select>
        <el-select v-model="class_third_id" placeholder="请选择三级分类" size="mini" class="mr10" @change="getDataPage">
          <el-option v-for="(item, index) in thirdlist" :key="item.ID" :label="item.name" :value="item.ID"></el-option>
        </el-select>
        <el-input v-model="keyword" placeholder="关键词" class="handle-input mr10" size="mini" style="width: 160px;"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="getDataPage" class="mr10" size="mini">搜索</el-button>
      </div>

      <el-table :data="tableData" class="table" ref="multipleTable" header-cell-class-name="table-header" border size="mini"
        @selection-change="handleSelectionChange" v-loading="loading" :height="$tableHight">
        <el-table-column type="index" width="55" label="序号" align="center"></el-table-column>
        <el-table-column prop="business.name" label="商家名称" width="120"></el-table-column>
        <el-table-column prop="name" label="商品名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="title" label="商品简介" show-overflow-tooltip></el-table-column>
        <el-table-column prop="price" label="商品价格" width="100"></el-table-column>
        <el-table-column prop="stock" label="商品库存" width="100"></el-table-column>
        <el-table-column prop="discount" label="会员让利（%）" width="120"></el-table-column>
        <el-table-column prop="platform_discount" label="平台让利（%）" width="120"></el-table-column>
        <el-table-column label="展示图片" align="center" width="90">
          <template slot-scope="scope">
            <el-image class="table-td-thumb" :src="scope.row.img" :preview-src-list="[scope.row.img]"></el-image>
          </template>
        </el-table-column>
        <el-table-column prop="sort" label="排序" width="80" align="center">
          <template slot-scope="scope">
            <el-input v-model="scope.row.sort" @blur="shortcutUpdateGoods(scope.row, 'sort')" size="mini"></el-input>
          </template>
        </el-table-column>
<!--        <el-table-column label="首页展示" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag :type="scope.row.is_index ? 'success' : 'danger'"-->
<!--              @click="shortcutUpdateGoods(scope.row, 'is_index')">{{-->
<!--                scope.row.is_index ? '是' : '否' }}</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="热销推荐" align="center" width="80">
          <template slot-scope="scope">
            <el-tag :type="scope.row.is_hot ? 'success' : 'danger'" size="mini" @click="shortcutUpdateGoods(scope.row, 'is_hot')">{{
              scope.row.is_hot ? '是' : '否' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="商品状态" align="center" width="90">
          <template slot-scope="scope">
            <el-tag size="mini" :type="scope.row.state == 3 ? 'danger' : scope.row.state == 1 ? 'success' : ''   ">{{ ['下架', '上架'
              , '待审核','审核失败'][scope.row.state] }}</el-tag>
          </template>
        </el-table-column>
<!--        <el-table-column label="热销推荐" align="center">-->
<!--          <template slot-scope="scope">-->
<!--            <el-tag :type="scope.row.is_extension ? 'success' : 'danger'"-->
<!--              @click="shortcutUpdateGoods(scope.row, 'is_extension')">{{-->
<!--                scope.row.is_extension ? '是' : '否' }}</el-tag>-->
<!--          </template>-->
<!--        </el-table-column>-->
        <el-table-column label="操作" width="160" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="goDetail(scope.row.ID)" size="mini">详情</el-button>
            <el-button type="text" v-if="scope.row.state == 2" @click="jujue(scope.row.ID, 1)" size="mini">拒绝</el-button>
            <el-button type="text" v-if="scope.row.state == 2" @click="grounding(scope.row.ID, 0)" size="mini">同意</el-button>
            <el-button type="text" v-if="scope.row.state == 1" @click="jujue(scope.row.ID, 1)" size="mini">下架</el-button>
            <el-button type="text" v-if="scope.row.state == 1"
              @click="copy('/pages/commodity/details?id=' + scope.row.ID)" size="mini">复制链接</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">

        <el-pagination @size-change="handleSizeChange" @current-change="handlePageChange"
          :current-page="query.pageIndex" :page-sizes="[20, 30, 40, 50]" :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper" :total="pageTotal">
        </el-pagination>
      </div>
    </div>
    </el-card>
  </div>
</template>

<script>
export default {
  name: 'basetable',
  data() {
    return {
      query: {
        pageIndex: 1,
        pageSize: 20
      },
      upimg: {
        token: '',
        key: new Date().valueOf()
      },
      code: 0,
      state: -1,
      upload: '',
      editVisible: false,
      tableData: [],
      pageTotal: 0,
      imageUrl: '',
      videoUrl: '',
      type: 0,
      navlist: [],
      keyword: '',
      loading: false,
      class_first_id: '',
      class_second_id: '',
      class_third_id: '',
      firstlist: [],
      secondlist: [],
      thirdlist: [],
    };
  },
  created() {
    this.getNAV()
  },
  methods: {
    copy(row) {
      // 创建一个input框
      const input = document.createElement('input')
      // 将指定的DOM节点添加到body的末尾
      document.body.appendChild(input)
      // 设置input框的value值为复制的值
      const address = row
      input.setAttribute('value', address)
      // 选取文本域中的内容
      input.select()
      // copy的意思是拷贝当前选中内容到剪贴板
      // document.execCommand（）方法操纵可编辑内容区域的元素
      // 返回值为一个Boolean，如果是 false 则表示操作不被支持或未被启用
      if (document.execCommand('copy')) {
        document.execCommand('copy')
      }
      // 删除这个节点
      document.body.removeChild(input)
      // 弹窗：复制成功
      this.$message.success('复制成功');
      //
    },
    //修改size值
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.query.pageSize = val
      this.getData();
    },
    // 修改商品信息
    shortcutUpdateGoods(e, type) {
      console.log(type)
      if (type == 'is_index') {
        e.is_index = e.is_index ? 0 : 1
      }
      if (type == 'is_hot') {
        console.log('is_hot',e.is_hot)
        e.is_hot =( e.is_hot==1 )? 0 : 1
      }
      if (type == 'is_extension') {
        e.is_extension = e.is_extension ? 0 : 1
      }
      if (type == 'state') {
        e.state = e.state ? 0 : 1
      }
      this.request('admin/shortcutUpdateGoods/', 'post', { id: e.ID, is_index: e.is_index, state: e.state, is_hot: e.is_hot, is_extension: e.is_extension, sort: Number(e.sort) }).then(res => {
        this.getData()
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
        } else {
          this.$message.error(res.errmsg);
        }
      })
    },

    //商品分区修改
    getoption(e) {
      this.code = e
    },
    //获取商品分区
    getNAV() {
      this.request('admin/partition/', 'get', { page: this.query.pageIndex, size: this.query.pageSize }).then(res => {
        this.navlist = [{ ID: 0, name: '全部' }, ...res.data.data]
        // this.code = res.data.data[0].ID

        this.getData()
      })
      this.request('admin/firstClass/', 'get', { page: 1, size: 100, keyword: '' }).then(res => {
        this.firstlist = res.data.data
      })
    },
    //获取2级分类
    setfirst() {
      this.class_second_id = ''
      this.request('admin/secondClass/', 'get', { class_first_id: this.class_first_id, page: 1, size: 100, keyword: '' }).then(res => {
        this.secondlist = res.data.data
        this.getData();
      })
    },
    getDataPage() {
      this.query.pageIndex = 1
      this.getData();

    },
    //获取3级分类
    setsecond() {
      this.class_third_id = ''
      this.request('admin/thirdClass/', 'get', { class_second_id: this.class_second_id, page: 1, size: 100, keyword: '' }).then(res => {
        this.thirdlist = res.data.data
        this.getData();
      })
    },
    //获取数据
    getData() {
      this.loading = true
      this.request('admin/businessGoodsList/', 'get', {
        state: this.state, page: this.query.pageIndex, size: this.query.pageSize, keyword: this.keyword, partition: this.code, class_first_id: this.class_first_id,
        class_second_id: this.class_second_id,
        class_third_id: this.class_third_id
      }).then(res => {
        console.log(res, "----")
        this.tableData = res.data.data
        this.pageTotal = res.data.total
      })
      this.loading = false
    },
    // 编辑
    handleEdit(e) {
      // console.log(e.content)
      this.$router.push({ path: '/commodity/addgoods', query: { id: e.ID } });
    },
    goDetail(id) {
      this.$router.push({ path: '/business/goodsDetail', query: { id: id } });
    },
    cancel() {
      this.getData();
    },
    //拒绝
    jujue(id, code) {
      this.$prompt('请输入拒绝原因', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
      }).then(({ value }) => {
        this.request('admin/businessGoodsList/', 'DELETE', { id: id, reason: value, code: code }).then(res => {
          if (res.errno == 0) {
            this.$message.success(res.errmsg);
          } else {
            this.$message.error(res.errmsg);
          }
        });
        this.getData();
      }).catch(() => {

      });

    },
    //同意上架
    grounding(id, code) {
      this.request('admin/businessGoodsList/', 'DELETE', { id: id, code: code }).then(res => {
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
        } else {
          this.$message.error(res.errmsg);
        }
        this.getData();
      });
    },
    // 修改排序
    operations(e, code) {
      // console.log(e)
      e.sort = Number(e.sort)
      // console.log(e)
      this.request('admin/add_news/', 'put', e).then(res => {
        if (res.errno == 0) {
          this.$message.success(res.errmsg);
        } else {
          this.$message.error(res.errmsg);
        }
      });
      this.editVisible = false;
      this.getData();
    },
    // 删除操作
    handleDelete(index, row) {
      // 二次确认删除
      this.$confirm('确定要删除吗？', '提示', {
        type: 'warning'
      })
        .then(() => {
          this.request('admin/add_goods/', 'delete', { ID: row.ID }).then(res => {
            if (res.errno == 0) {
              this.$message.success(res.errmsg);
              this.tableData.splice(index, 1)
              this.getData()
            } else {
              this.$message.error(res.errmsg);
            }
          });
        })
    },
    // 多选操作
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    // 分页导航
    handlePageChange(val) {
      this.$set(this.query, 'pageIndex', val);
      this.getData();
    }
  }
};
</script>

<style scoped>
.container {
  padding: 20px;
}

.el-upload--text {
  width: 200px !important;
}

.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;

  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  /* width: 100px; */
  height: 100px;
  line-height: 100px;
  text-align: center;
}

.avatar {
  width: 100%;
  height: 178px;
  display: block;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 30px;
  height: 30px;
  border-radius: 2px;
  .el-image img {
    object-fit: cover;
  }
}

/* 设置分页按钮样式 */
.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}
/* 使用 /deep/ 或 >>> 来穿透 scoped 样式 */
::v-deep .el-table th.el-table__cell {
  background-color: #f2f2f2;
  /* 设置表头颜色 */
  color: #333;
  /* 设置表头文字颜色 */
}
</style>
